package algo;

/**
 * @author Ajie
 * @date 8/18/24
 * @function 求均值。
 */
public class StreamingMean {

    private double filteredA_mid_value = -52;//TODO  16可以
//    private double filteredA_mid_value = -57;//TODO  13 数据可以
//    private double filteredB_mid_value = -56;
//    private double filteredA_mid_value = -53;//TODO
//    private double filteredB_mid_value = -52;

    //    private double filteredA_mid_value = -57;//TODO  13 数据可以...16不可以
//    private double filteredB_mid_value = -58;

    private double mean;
    private int count;

    public StreamingMean() {
        this.mean = 0.0;
        this.count = 0;
    }

    /**
     * 更新均值计算
     * @param newValue 新数据点
     */
    public void update(double newValue) {
        if (newValue>=-1 || newValue <= -89)return;
        count++;
        mean += (newValue - mean) / count;
    }

    /**
     * 获取当前计算的均值
     * @return 当前均值
     */
    public double getMean() {
        if (count < 500 ){
            return filteredA_mid_value;
        }
        return mean;
    }

    public static void main(String[] args) {
        // 假设我们有一个流式数据源
        double[] dataStream = {-46.0, -39.0, -41.0, -41.0, -46.0, -43.0, -41.0, -42.0, -41.0, -39.0, -46.0, -47.0, -48.0};

        // 创建流式均值计算器
        StreamingMean streamingMean = new StreamingMean();

        // 模拟数据流的到来
        for (double value : dataStream) {
            streamingMean.update(value);
            System.out.println("Updated mean: " + streamingMean.getMean());
        }

        // 最终均值
        System.out.println("Final mean: " + streamingMean.getMean());
    }
}